class Person:
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight

    def __str__(self):
        return "我叫%s,体重%.2f斤" % (self.name, self.weight)

    def run(self):
        print("%s爱跑步" % self.name)
        # 体重减少
        self.weight -= 0.5

    def eat(self):
        print("%s爱吃饭" % self.name)
        self.weight += 0.5


p = Person("小明", 180)
p.eat()
p.run()
print(p)


# 房子与家具类
class HouseItem:

    def __init__(self, name, area) -> None:
        self.name = name
        self.area = area

    def __str__(self) -> str:
        return "占地%.2f的%s" % (self.area, self.name)


class House:
    def __init__(self, house_type, area) -> None:
        # 户型
        self.house_type = house_type
        # 面积
        self.area = area
        # 剩余面积
        self.free_area = area
        # 家具列表
        self.item_list = []

    def __str__(self) -> str:
        # 如果一行代码过长可以将代码添加一对小括号然后再换行，python会自动将括号中的代码连接在一起
        return ("【%s】面积为%.2f,剩余面积为%.2f\n家具有:%s"
                % (self.house_type, self.area,
                   self.free_area, self.item_list))

    def show_item(self):
        print(self.item_list)

    def add_item(self, item):
        """
        添加家具
        :param item: 家具
        :return:
        """
        # 判断是否可以添加家具
        if item.area <= self.free_area:
            self.item_list.append(item.name)
            # 减少剩余面积
            self.free_area -= item.area
        else:
            print("装不下%s了" % item.name)


house = House("三居室", 100)
bed = HouseItem("席梦思", 4)
chest = HouseItem("衣柜", 2)
table = HouseItem("餐桌", 1.5)
house.add_item(table)
print(house)
print(house.item_list)


# 封装案例 士兵与枪
class Gun:
    def __init__(self, model) -> None:
        self.model = model
        self.bullet = 0

    def add_bullet(self, count):
        """
        装填子弹
        :param count:
        :return:
        """
        self.bullet += count

    def shoot(self):
        if self.bullet <= 0:
            print("没子弹了")
            return
        print("%s射出子弹" % self.model)
        self.bullet -= 1


class Soldier:
    def __init__(self, name) -> None:
        self.name = name
        self.gun = None

    def fire(self):
        if self.gun is None:
            print("没有枪")
            return
        # 装弹
        self.gun.add_bullet(2)
        print("%s开始射击" % self.name)
        self.gun.shoot()


gun = Gun("AK47")

soldier = Soldier("许三多")
soldier.gun = gun
soldier.fire()


# 身份运算符 is、is not判断两个对象内存地址是否一致是否是同一个对象

# 私有属性、私有方法
class MyUtils:
    def __init__(self, name) -> None:
        self.name = name
        # 属性前添加__定义为私有属性
        self.__age = 18

    def __test_utils(self):
        """
        私有方法,外界无法访问
        :return:
        """
        print("我叫%s,今年%d岁" % (self.name, self.__age))


utils = MyUtils("slx")
# 如果外界需要访问可以使用 对象._类名__私有属性/方法调用
print(utils._MyUtils__age)
